package superhero.gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.border.Border;
import superhero.data.SuperheroDBQuery;

/**
 *
 * @author computer
 */
public class CreateNewTab extends JFrame
{
    private JButton editButton, saveButton;
    private JPanel gridLabels, temp, newTab;
    private JPanel heroPanel = new JPanel(new BorderLayout());
    private JLabel holder;
    private JTextArea[] textFields = new JTextArea[3];
    private JTextArea textArea = new JTextArea();
    private JLabel[] labels = new JLabel[3];
    
    /*
     * This class is called whenever the SEARCH button is clicked in the
     * SuperheroDisplayPanel. It will create a new TAB for each character
     * selected. 
     */
    public CreateNewTab(Object name)
    {
            temp = new JPanel();
            gridLabels = new JPanel(new GridLayout(3,2));
            gridLabels.setVisible(true);
            JPanel[] tempPanels = new JPanel[3];
            String[] labelNames = {"Character Name:", "Real Name:", "Powers:"};
            String query = "Select ";
            query += " characters.CharacterName, "
                    + "characters.RealName, characters.Powers"
                    + " FROM Characters WHERE CharacterName = '"
                    + name.toString() + "'";

            SuperheroDBQuery dbQuery = new SuperheroDBQuery(query);

            String[][] data = dbQuery.getTableData();

            Font font = new Font("Verdana", Font.BOLD, 12);
            Border border = BorderFactory.createLineBorder(Color.BLACK);
            for (int i = 0; i < 3; i++) 
            {
                tempPanels[i] = new JPanel();
                labels[i] = new JLabel(labelNames[i]);
                textFields[i] = new JTextArea();
                textFields[i].setEditable(false);
//                textFields[i].setHorizontalAlignment(JTextField.CENTER);
                tempPanels[i].add(labels[i]);
                gridLabels.add(tempPanels[i]);
                gridLabels.add(textFields[i]);
                textFields[i].setText("           " +data[0][i].toString());
                textFields[i].setFont(font);
                textFields[i].setBackground(Color.ORANGE);
                textFields[i].setBorder(border);
            }

            textArea.setText(textFields[2].getText());
            gridLabels.remove(textFields[2]);
            gridLabels.add(textArea);
            textArea.setWrapStyleWord(true);
            textArea.setLineWrap(true);
            textArea.setFont(font);
            textArea.setBackground(Color.ORANGE);
            textArea.setBorder(border);

            heroPanel.add(gridLabels,
                          BorderLayout.CENTER);
            
            String fileName = "Images//" + name.toString().trim() + ".jpg";
            ImageIcon image = new ImageIcon(fileName);
            holder = new JLabel(image);
            editButton = new JButton("Edit Hero Info");
            editButton.addActionListener(new editButtonListener());

            heroPanel.add(holder,
                          BorderLayout.WEST);
            temp.add(editButton);
            
            heroPanel.add(temp,
                          BorderLayout.SOUTH);
            heroPanel.setVisible(true);
            newTab = new JPanel();
            pack();       
    }

    /**
     * @return the heroPanel
     */
    public JPanel getHeroPanel()
    {
        return heroPanel;
    }

    /**
     * @return the newTab
     */
    public JPanel getNewTab()
    {
        newTab.add(heroPanel);
        return newTab;
    }



    private class editButtonListener
            implements ActionListener
    {

        @Override
        public void actionPerformed(ActionEvent ae)
        {
            heroPanel.removeAll();
            heroPanel.add(gridLabels,
                               BorderLayout.CENTER);

            heroPanel.add(holder,
                               BorderLayout.WEST);

            heroPanel.add(temp,
                               BorderLayout.SOUTH);
            getHeroPanel().setVisible(true);

            for (int i = 0;
                    i < textFields.length;
                    i++) {
                textFields[i].setEditable(true);                
                textFields[i].setBackground(Color.WHITE);
            }
            textFields[0].setEditable(false);
            textArea.setEditable(true);
            textArea.setBackground(Color.WHITE);

            temp.remove(editButton);
            saveButton = new JButton("Save");
            saveButton.addActionListener(new saveButtonListener());
            temp.add(saveButton);
            pack();
        }
    }

    private class saveButtonListener
            implements ActionListener
    {

        @Override
        public void actionPerformed(ActionEvent ae)
        {                    
            SuperheroDBQuery update = new SuperheroDBQuery();
            update.updateRecord(textFields[0].getText().trim(),
                                textFields[1].getText().trim(),
                                textArea.getText().trim());
            update.closeConnection();    

            JOptionPane.showMessageDialog(temp,
                                          "New information is now saved!");
            heroPanel.removeAll();
            heroPanel.add(gridLabels,
                               BorderLayout.CENTER);

            heroPanel.add(holder,
                               BorderLayout.WEST);

            heroPanel.add(temp,
                               BorderLayout.SOUTH);
            getHeroPanel().setVisible(true);

            for (int i = 0;
                    i < textFields.length;
                    i++) {
                textFields[i].setEditable(false);
                textFields[i].setText("           " + textFields[i].getText());
                textFields[i].setBackground(Color.ORANGE);
            }
            textFields[0].setEditable(false);
            textArea.setText(textFields[2].getText());
            textArea.setEditable(false);
            textArea.setBackground(Color.ORANGE);

            temp.remove(saveButton);
            temp.add(editButton);
            pack();
        }
    }
}
